{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find the Longest Balanced Substring of a Binary String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findTheLongestBalancedSubstring"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最长平衡子字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个仅由 <code>0</code> 和 <code>1</code> 组成的二进制字符串 <code>s</code> 。<span style=\"\">&nbsp;</span><span style=\"\">&nbsp;</span></p>\n",
    "\n",
    "<p>如果子字符串中 <strong>所有的<span style=\"\"> </span></strong><code><span style=\"\">0</span></code><strong><span style=\"\"> </span>都在 </strong><code>1</code><strong> 之前</strong> 且其中 <code>0</code> 的数量等于 <code>1</code> 的数量，则认为 <code>s</code> 的这个子字符串是平衡子字符串。请注意，空子字符串也视作平衡子字符串。<span style=\"\">&nbsp;</span></p>\n",
    "\n",
    "<p>返回&nbsp;<span style=\"\"> </span><code>s</code> 中最长的平衡子字符串长度。</p>\n",
    "\n",
    "<p>子字符串是字符串中的一个连续字符序列。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"01000111\"\n",
    "<strong>输出：</strong>6\n",
    "<strong>解释：</strong>最长的平衡子字符串是 \"000111\" ，长度为 6 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"00111\"\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>最长的平衡子字符串是 \"0011\" ，长度为 <span style=\"\">&nbsp;</span>4 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"111\"\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>除了空子字符串之外不存在其他平衡子字符串，所以答案为 0 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 50</code></li>\n",
    "\t<li><code>'0' &lt;= s[i] &lt;= '1'</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-the-longest-balanced-substring-of-a-binary-string](https://leetcode.cn/problems/find-the-longest-balanced-substring-of-a-binary-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-the-longest-balanced-substring-of-a-binary-string](https://leetcode.cn/problems/find-the-longest-balanced-substring-of-a-binary-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"01000111\"', '\"0011\"', '\"111\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n, idx, ans = len(s), 0, 0\n",
    "        while idx < n:\n",
    "            a, b = 0, 0\n",
    "            while idx < n and s[idx] == '0':\n",
    "                a, idx = a + 1, idx + 1\n",
    "            while idx < n and s[idx] == '1':\n",
    "                b, idx = b + 1, idx + 1\n",
    "            ans = max(ans, min(a, b) * 2)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        ans = pre = cur = 0\n",
    "        for i, c in enumerate(s):\n",
    "            cur += 1\n",
    "            if i == len(s) - 1 or c != s[i + 1]:\n",
    "                if c == '1':\n",
    "                    ans = max(ans, 2 * min(pre, cur))\n",
    "                pre = cur\n",
    "                cur = 0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        output = 0\n",
    "        while i < n:\n",
    "            c1 = 0\n",
    "            c2 = 0\n",
    "            while i < n and s[i] == '0':\n",
    "                c1 += 1\n",
    "                i += 1\n",
    "            while i < n and s[i] == '1':\n",
    "                c2 += 1\n",
    "                i += 1\n",
    "            output = max(output, min(c1, c2))\n",
    "        return output * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0, 0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i-1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0, 0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i-1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        count_0 = 0\n",
    "        count_1 = 0\n",
    "        res = 0\n",
    "        for i in s:\n",
    "            if i == '0':\n",
    "                if count_1 != 0:\n",
    "                    res = max(res, min(count_1, count_0))\n",
    "                    count_1 = 0\n",
    "                    count_0 = 0\n",
    "                count_0 += 1\n",
    "            else:\n",
    "                count_1 += 1\n",
    "        res = max(res, min(count_1, count_0))\n",
    "        return res*2\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0,0]\n",
    "        for i in range(n):\n",
    "            if s[i]== '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i - 1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        s = s + '0'\n",
    "        count0 = 0\n",
    "        count1 = 0\n",
    "        t = 0\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i-1] == '0':\n",
    "                count0 += 1\n",
    "            if s[i-1] == '1':\n",
    "                count1 += 1\n",
    "            if s[i-1] == '1' and s[i] == '0':\n",
    "                t = max(t,min(count0,count1))\n",
    "                count0 = 0\n",
    "                count1 = 0\n",
    "        return 2*t\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        ans = last = cur = 0\n",
    "        for i, c in enumerate(s):\n",
    "            cur += 1\n",
    "            if i == len(s)-1 or c != s[i+1]:\n",
    "                if c == '1':\n",
    "                    ans = max(ans, 2*min(last, cur))\n",
    "                last = cur\n",
    "                cur = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = i = zeros = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == '1':\n",
    "                if zeros == 0:\n",
    "                    i += 1\n",
    "                else:\n",
    "                    j = i \n",
    "                    ones = 0\n",
    "                    while j < len(s) and s[j] == '1':\n",
    "                        ones += 1\n",
    "                        j += 1\n",
    "                    res = max(res, 2 * min(zeros, ones))\n",
    "                    i = j\n",
    "                    zeros = 0\n",
    "            else:\n",
    "                zeros += 1\n",
    "                i += 1\n",
    "\n",
    "        return res\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        ind = 0\n",
    "        substr = []\n",
    "        ans = 0\n",
    "        while ind < len(s):\n",
    "            if s[ind] == '0':\n",
    "                substr.append(0)\n",
    "                ind += 1\n",
    "            else:\n",
    "                if s[ind:ind + len(substr)] == '1' * len(substr):\n",
    "                    ans = max(ans, len(substr) * 2)\n",
    "                    substr = []\n",
    "                    ind += 1\n",
    "                else:\n",
    "                    ind -= len(substr) - 1\n",
    "                    substr = []\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def findTheLongestBalancedSubstring(self, s):\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        while i < n:\n",
    "            a = 0\n",
    "            b = 0\n",
    "            while i < n and s[i] == '0':\n",
    "                a += 2\n",
    "                i += 1\n",
    "            while i < n and s[i] == '1':\n",
    "                b += 2\n",
    "                i += 1\n",
    "            ans = max(ans, min(a, b))\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0, 0]\n",
    "        for i in range(n):\n",
    "            if s[i] == \"1\":\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i - 1] == \"1\":\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        # 记连续0 和连续1\n",
    "        consec0 = consec1 = res = 0\n",
    "        prev = '2'\n",
    "\n",
    "        for c in s:\n",
    "            if c == prev:\n",
    "                if c == '0':\n",
    "                    consec0 += 1\n",
    "                else:\n",
    "                    consec1 += 1\n",
    "            else:\n",
    "                if c == '0':\n",
    "                    res = max(res, min(consec0, consec1) << 1)\n",
    "                    consec0 = 1\n",
    "                    consec1 = 0\n",
    "                else:\n",
    "                    consec1 = 1\n",
    "\n",
    "            prev = c\n",
    "\n",
    "        res = max(res, min(consec0, consec1) << 1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        result = 0\n",
    "        zeroNumber = 0\n",
    "        oneNumber = 0\n",
    "        size = len(s)\n",
    "        for index in range(size):\n",
    "            if s[index] == '1':\n",
    "                oneNumber += 1\n",
    "                result = max(result, 2 * min(zeroNumber, oneNumber))\n",
    "            elif index == 0 or s[index - 1] == '1':\n",
    "                oneNumber = 0\n",
    "                zeroNumber = 1\n",
    "            else:\n",
    "                zeroNumber += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        # 10(left)001(mid)10(right)10\n",
    "        try:\n",
    "            while s[0] == \"1\":\n",
    "                s = s[1:]\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "            return 0\n",
    "        if s[-1] == \"1\":\n",
    "            s += \"0\"\n",
    "        res = 0\n",
    "        cont_0 = 0\n",
    "        cont_1 = 0\n",
    "        flg = \"1\"\n",
    "        for i, c in enumerate(s):\n",
    "            if flg == \"0\" and c == \"0\":\n",
    "                cont_0 += 1\n",
    "            elif flg == \"1\" and c == \"1\":\n",
    "                cont_1 += 1\n",
    "            elif flg == \"0\" and c == \"1\":\n",
    "                flg = \"1\"\n",
    "                cont_1 += 1\n",
    "            else:  # flg==\"1\" and c==\"0\":\n",
    "                cur_res = min(cont_0, cont_1) * 2\n",
    "                if cur_res > res:\n",
    "                    res = cur_res\n",
    "                flg = \"0\"\n",
    "                cont_0 = 1\n",
    "                cont_1 = 0\n",
    "            tt = 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0,0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1]+=1\n",
    "                res=max(res,2*min(count))\n",
    "            elif i==0 or s[i-1]=='1':\n",
    "                count[0]=1\n",
    "                count[1]=0\n",
    "            else:\n",
    "                count[0]+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n,idx,ans =len(s),0,0\n",
    "        while idx<n:\n",
    "            a,b=0,0\n",
    "            while idx<n and s[idx]=='0':\n",
    "                a,idx = a+1,idx+1\n",
    "            while idx<n and s[idx]=='1':\n",
    "                b,idx = b+1,idx+1\n",
    "            ans = max(ans,min(a,b)*2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        num_of_01 = [0, 0]\n",
    "        for now in s:\n",
    "            if now == '0' and num_of_01[1] == 0:\n",
    "                num_of_01[0] += 1\n",
    "                continue\n",
    "            if now == '0' and num_of_01[1] != 0:\n",
    "                res = max(res, 2 * min(num_of_01[0], num_of_01[1]))\n",
    "                num_of_01 = [1, 0]\n",
    "                continue\n",
    "            if now == '1' and num_of_01[0] != 0:\n",
    "                num_of_01[1] += 1\n",
    "                continue\n",
    "        res = max(res, 2 * min(num_of_01[0], num_of_01[1]))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        ans = 0\n",
    "        while i < n:\n",
    "            if s[i] == '1':\n",
    "                i += 1\n",
    "            else:\n",
    "                break\n",
    "        \n",
    "        while i < n:\n",
    "            cnt0 = cnt1 = 0\n",
    "            while i < n and s[i] == '0':\n",
    "                cnt0 += 1\n",
    "                i += 1\n",
    "            while i < n and s[i] == '1':\n",
    "                cnt1 += 1\n",
    "                i += 1\n",
    "            ans = max(2 * min(cnt0, cnt1), ans)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0, 0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i-1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        if s.isspace():\n",
    "            return len(s)\n",
    "        elif s.count(\"1\")==0:\n",
    "            return 0\n",
    "        else:\n",
    "            num = list(s)\n",
    "            answer = 0\n",
    "            num.append(\"end\")\n",
    "            while len(num) != 1:\n",
    "                if num.count(\"1\") == 0:\n",
    "                    return answer \n",
    "                mid = num.index(\"1\")\n",
    "                i = mid\n",
    "                count = 0\n",
    "                while num[i] == \"1\":\n",
    "                    count += 1\n",
    "                    i += 1\n",
    "                n = min(mid, count)\n",
    "                m = max(mid, count)\n",
    "                if mid >= 1:\n",
    "                    if min(mid, count)*2 > answer:\n",
    "                        answer = n*2\n",
    "                for j in range(m):\n",
    "                    del num[0]\n",
    "            return answer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        for i in range(25, -1, -1):\n",
    "            if '0' * i + '1' * i in s:\n",
    "                return i * 2\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        length = len(s)\n",
    "        i = 0\n",
    "        while i < length:\n",
    "            count_of_zero = 0\n",
    "            count_of_one = 0\n",
    "            while i < length and s[i] == '0':\n",
    "                count_of_zero += 1\n",
    "                i += 1\n",
    "            while i < length and s[i] == '1':\n",
    "                count_of_one += 1\n",
    "                i += 1\n",
    "            ans = max(ans, 2 * min(count_of_zero, count_of_one))\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        num_of_01 = [0, 0]\n",
    "        for now in s:\n",
    "            if now == '0' and num_of_01[1] == 0:\n",
    "                num_of_01[0] += 1\n",
    "                continue\n",
    "            if now == '0' and num_of_01[1] != 0:\n",
    "                res = max(res, 2 * min(num_of_01[0], num_of_01[1]))\n",
    "                num_of_01 = [1, 0]\n",
    "                continue\n",
    "            if now == '1':\n",
    "                num_of_01[1] += 1\n",
    "                continue\n",
    "        res = max(res, 2 * min(num_of_01[0], num_of_01[1]))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0, 0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i-1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\r\n",
    "        zero = 0\r\n",
    "        one = 0\r\n",
    "        res = 0\r\n",
    "        n = len(s)\r\n",
    "        for i in range(n):\r\n",
    "            if s[i]=='1':\r\n",
    "                one+=1\r\n",
    "                res = max(res,2*min(zero,one))\r\n",
    "            elif i==0 or s[i-1]=='1':\r\n",
    "                one = 0\r\n",
    "                zero = 1\r\n",
    "            else:\r\n",
    "                zero += 1\r\n",
    "        return res\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        def check(i, j):\n",
    "            cnt = 0\n",
    "            for k in range(i, j + 1):\n",
    "                if s[k] == '1':\n",
    "                    cnt += 1\n",
    "                elif cnt:\n",
    "                    return False\n",
    "            return cnt * 2 == (j - i + 1)\n",
    "\n",
    "        n = len(s)\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if check(i, j):\n",
    "                    ans = max(ans, j - i + 1)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        magic = \"01\"\n",
    "        if magic not in s: return 0\n",
    "        while magic in s:\n",
    "            magic = \"0\" + magic + \"1\"\n",
    "        return len(magic) - 2\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        num = '01'\n",
    "        if num not in s:\n",
    "            return 0\n",
    "        while num in s:\n",
    "            num = '0' + num + '1'\n",
    "        \n",
    "        return len(num)-2\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        dp=[0]*len(s)\n",
    "        ans=0\n",
    "        for i in range(len(s)):\n",
    "            if i==0:\n",
    "                dp[i]=0\n",
    "            elif s[i]=='0':\n",
    "                dp[i]=0\n",
    "            else:\n",
    "                if dp[i-1]==0:\n",
    "                    if s[i-1]=='0':\n",
    "                        dp[i]=2\n",
    "                else:\n",
    "                    if i-1>=dp[i-1]:\n",
    "                        if s[i-1-dp[i-1]]=='0':\n",
    "                            dp[i]=dp[i-1]+2\n",
    "            ans=max(ans,dp[i])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n, index, i = len(s), 0, 0\n",
    "        while index < n:\n",
    "            a, b = 0, 0\n",
    "            while index < n and s[index] == '0':\n",
    "                a, index = a + 1, index + 1\n",
    "            while index < n and s[index] == '1':\n",
    "                b, index = b + 1, index + 1\n",
    "            i = max(i, min(a, b) * 2)\n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        for i in range(0, n):\n",
    "            oneCount = 0\n",
    "            zeroCount = 0\n",
    "            for j in range(i, n):\n",
    "                if s[j] == '0' and oneCount >= 1:\n",
    "                    break\n",
    "                if s[j] == '0':\n",
    "                    zeroCount += 1\n",
    "                else:\n",
    "                    oneCount += 1\n",
    "                if zeroCount == oneCount:\n",
    "                    res = max(res, j - i + 1)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        num0 = 0\n",
    "        num1 = 0\n",
    "        ans = 0\n",
    "        for i in range(len(s)):\n",
    "            if i == 0:\n",
    "                if s[i] == '0':\n",
    "                    num0 += 1\n",
    "            elif s[i] == '0':\n",
    "                if s[i - 1] == '0':\n",
    "                    num0 += 1\n",
    "                else:\n",
    "                    ans = max(ans, 2 * min(num0, num1))\n",
    "                    num1 = 0\n",
    "                    num0 = 1\n",
    "            else:\n",
    "\n",
    "                num1 += 1\n",
    "                if i == (len(s) - 1):\n",
    "                    ans = max(ans, 2 * min(num0, num1))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n, idx, ans = len(s), 0, 0\n",
    "        while idx < n:\n",
    "            a, b = 0, 0\n",
    "            while idx < n and s[idx] == '0':\n",
    "                a, idx = a + 1, idx + 1\n",
    "            while idx < n and s[idx] == '1':\n",
    "                b, idx = b + 1, idx + 1\n",
    "            ans = max(ans, min(a, b) * 2)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n0,n1,l = 0,0,len(s)\n",
    "        ans = 0\n",
    "        for i in range(l+1):\n",
    "            if i == l:\n",
    "                ans = max(ans,min(n0,n1)*2)\n",
    "            else:\n",
    "                if s[i] == '0':\n",
    "                    if n1 != 0:\n",
    "                        ans = max(ans,min(n0,n1)*2)\n",
    "                        n1 = 0\n",
    "                        n0 = 1\n",
    "                    else:\n",
    "                        n0 += 1\n",
    "                else:\n",
    "                    n1 += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def findTheLongestBalancedSubstring(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret, n = 0, len(s)\n",
    "        count = [0,0]\n",
    "        for i in range(n):\n",
    "            if s[i]=='1':\n",
    "                count[1] += 1\n",
    "                ret = max(ret, 2*min(count))\n",
    "            elif i==0 or s[i-1] == '1':\n",
    "                count = [1, 0]\n",
    "            else:\n",
    "                count[0] += 1\n",
    "\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        sample = \"01\"\n",
    "        while s.find(sample) >= 0:\n",
    "            sample = \"0\" + sample + \"1\"\n",
    "        return len(sample) - 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        ans = 0\n",
    "        for l in range(2,n+1,2):\n",
    "            for i in range(n):\n",
    "                j = l+i-1\n",
    "                if j>=n:\n",
    "                    break\n",
    "                if s[i:i+l//2]=='0'*(l//2) and s[i+l//2:j+1]=='1'*(l//2):\n",
    "                    ans = l\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        # i = 0\n",
    "        # res = 0\n",
    "        # sum0 = 0\n",
    "        # sum1 = 1\n",
    "        # while True:\n",
    "        #     while i < len(s) and s[i]=='0':\n",
    "        #         sum0 += 1\n",
    "        #         i += 1\n",
    "        #     while i < len(s) and s[i]=='1':\n",
    "        #         sum1 += 1\n",
    "        #         i += 1\n",
    "        #     res = max(res, min(sum0, sum1))\n",
    "        #     if i >= len(s):\n",
    "        #         break\n",
    "        #     sum0 = 0\n",
    "        #     sum1 = 0\n",
    "        # return res * 2\n",
    "        i=0\n",
    "        res=0\n",
    "        sum0,sum1=0,0\n",
    "        n=len(s)\n",
    "        while True:\n",
    "            while i< n and s[i]=='0':\n",
    "                sum0+=1\n",
    "                i+=1\n",
    "            while i<n and s[i]=='1':\n",
    "                sum1+=1\n",
    "                i+=1\n",
    "            res=max(res,min(sum0,sum1))\n",
    "            if i >= n:\n",
    "                break\n",
    "            sum0=0\n",
    "            sum1=0\n",
    "        return 2*res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # hashmap, start, max_len = {}, 0, 0\n",
    "        # for i in range(len(s)):\n",
    "        #     hashmap[s[i]] = hashmap.get(s[i], 0) + 1\n",
    "        #     if hashmap.get('0', 0) == hashmap.get('1', 0):\n",
    "        #         max_len = max(max_len, i - start + 1)\n",
    "        #     while hashmap.get('0', 0) != hashmap.get('1', 0):\n",
    "        #         hashmap[s[start]] -= 1\n",
    "        #         if hashmap[s[start]] == 0:\n",
    "        #             del hashmap[s[start]]\n",
    "        #         start += 1\n",
    "        # return max_len\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        cnt0, cnt1 = 0, 0\n",
    "        res = 0\n",
    "        if s[0]=='0':\n",
    "            cnt0 += 1\n",
    "        for i in range(1,len(s)):\n",
    "            # 如果是10组合\n",
    "            if s[i-1]=='1' and s[i]=='0':\n",
    "                res = max(res, min(cnt0,cnt1)*2)\n",
    "                cnt0, cnt1 = 1, 0\n",
    "            else:\n",
    "                if s[i]=='0':\n",
    "                    cnt0 += 1\n",
    "                else:\n",
    "                    cnt1 += 1\n",
    "        return max(res, min(cnt0,cnt1)*2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0, 0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i-1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        # 初始化字符串长度、遍历索引、最长的平衡子字符串长度\n",
    "        n = len(s)\n",
    "        idx = 0\n",
    "        ans = 0\n",
    "\n",
    "        # 循环遍历字符串\n",
    "        while idx < n:\n",
    "            # 初始化连续 0 和连续 1 的个数\n",
    "            zeros = 0\n",
    "            ones = 0\n",
    "            # 找到连续0的个数\n",
    "            while idx < n and s[idx] == \"0\":\n",
    "                zeros += 1\n",
    "                idx += 1\n",
    "            # 找到连续1的个数\n",
    "            while idx < n and s[idx] == \"1\":\n",
    "                ones += 1\n",
    "                idx += 1\n",
    "            # 更新最长的平衡子字符串长度\n",
    "            ans = max(ans, min(zeros, ones) * 2)  \n",
    "\n",
    "        # 返回最长的平衡子字符串长度\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        pre = cur = ans = 0\n",
    "        n = len(s)\n",
    "        for i, c in enumerate(s):\n",
    "            cur += 1\n",
    "            if i == n - 1 or s[i] != s[i + 1]:\n",
    "                if c == '1':\n",
    "                    ans = max(ans, min(cur, pre) * 2)\n",
    "                pre = cur\n",
    "                cur = 0\n",
    "                \n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        if '0' not in s or '1' not in s:\n",
    "            return 0\n",
    "        left = s.index('0')\n",
    "        right = left\n",
    "        answer = 0\n",
    "        while left<len(s) and right<len(s):\n",
    "            while right<len(s) and s[right] != '1':\n",
    "                right += 1\n",
    "            if right>=len(s) or s[right] == '0':\n",
    "                break\n",
    "            count_0 = right-left\n",
    "            while right<len(s) and s[right] != '0':\n",
    "                right += 1\n",
    "            count_1 = right-left-count_0\n",
    "            if min(count_0,count_1)*2 > answer:\n",
    "                answer = min(count_0,count_1)*2\n",
    "            left = right\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        # 法1：双指针\n",
    "        # if len(s) == 0 or len(s) == 1:\n",
    "        #     return 0\n",
    "\n",
    "        # targetleft = 0\n",
    "        # targetright = 0\n",
    "\n",
    "        # for i in range(len(s)):\n",
    "        #     if s[i] == 0:\n",
    "        #         targetleft = i\n",
    "        #         targetright = i\n",
    "        #         break\n",
    "        \n",
    "        # while targetright <len(str)-1:\n",
    "        #     if s[targetright] == 1 and s[targetright + 1] == 0:\n",
    "\n",
    "\n",
    "        # 法2\n",
    "        result = 0\n",
    "        index = -1\n",
    "        num0 = 0\n",
    "        num1 = 0\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if int(s[i]) == 0:\n",
    "                index = i\n",
    "                break\n",
    "        if index == -1:\n",
    "            return 0\n",
    "        \n",
    "        while index < len(s):\n",
    "            if int(s[index]) == 0:\n",
    "                if num1 == 0:\n",
    "                    num0 += 1\n",
    "                else:\n",
    "                    result = max(min(num0,num1) * 2, result)\n",
    "                    num0 = 1\n",
    "                    num1 = 0\n",
    "            else:\n",
    "                num1 += 1\n",
    "                if index == len(s) - 1:\n",
    "                    result = max(min(num0,num1) * 2, result)          \n",
    "            index += 1\n",
    "        \n",
    "        return result\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        ans = pre = cur = 0\n",
    "        for i, c in enumerate(s):\n",
    "            cur += 1\n",
    "            if i == len(s) - 1 or c != s[i + 1]: \n",
    "                if c == '1':\n",
    "                    ans = max(ans, min(pre, cur) * 2)\n",
    "                pre = cur\n",
    "                cur = 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0, 0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i-1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        count=[]\n",
    "        pre='0'\n",
    "        temp_cnt=0\n",
    "        for i in range(len(s)):\n",
    "            if s[i]==pre:\n",
    "                temp_cnt+=1\n",
    "            else:\n",
    "                pre=s[i]\n",
    "                count.append(temp_cnt)\n",
    "                temp_cnt=1\n",
    "        count.append(temp_cnt)\n",
    "        temp_cnt=0\n",
    "        i=0\n",
    "        while i<len(count)-1:\n",
    "            temp_cnt=max(temp_cnt,min(count[i],count[i+1]))\n",
    "            i+=2\n",
    "        return 2*temp_cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n = len(s) // 2\n",
    "        for i in range(n, -1, -1):\n",
    "            t = \"0\" * i + \"1\" * i\n",
    "            if t in s:\n",
    "                return i * 2\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0, 0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i-1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        hl = n//2\n",
    "        while hl:\n",
    "            substr = '0'*hl+'1'*hl\n",
    "            if substr in s:\n",
    "                return hl*2\n",
    "            hl-=1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        max_len = 0\n",
    "        for c in s:\n",
    "            if b > 0 and c == \"0\":\n",
    "                max_len = max(max_len, min(a, b) * 2)\n",
    "                a = 0\n",
    "                b = 0\n",
    "            if c == \"0\":\n",
    "                a += 1\n",
    "            else:\n",
    "                b += 1\n",
    "        return max(max_len, min(a, b) * 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        tmp = \"\"\n",
    "\n",
    "        while len(tmp) < len(s) and tmp in s:\n",
    "            tmp = '0' + tmp + '1'\n",
    "            if tmp not in s:\n",
    "                return len(tmp) - 2\n",
    "        return len(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n, idx, ans = len(s), 0, 0\n",
    "        while idx < n:\n",
    "            a, b = 0, 0\n",
    "            while idx < n and s[idx] == '0':\n",
    "                a, idx = a + 1, idx + 1\n",
    "            while idx < n and s[idx] == '1':\n",
    "                b, idx = b + 1, idx + 1\n",
    "            ans = max(ans, min(a, b) * 2)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        matching_str=\"01\"\n",
    "        window=[0,1]\n",
    "        ans=0\n",
    "        while window[1]<len(s):\n",
    "            if s[window[0]:window[1]+1]==matching_str:\n",
    "                ans=window[1]-window[0]+1\n",
    "                window[0]-=1\n",
    "                window[1]+=1\n",
    "                if window[0]<0:\n",
    "                    window[0]+=1\n",
    "                    window[1]+=1\n",
    "                matching_str=\"0\"+matching_str+\"1\"\n",
    "            else:\n",
    "                window[0]+=1\n",
    "                window[1]+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0, 0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count))\n",
    "            elif i == 0 or s[i-1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        pre = 0 ;pre1 = 0\n",
    "        for str1 in s:\n",
    "            if str1 == '0':\n",
    "                if pre1:\n",
    "                    pre = pre1 =0\n",
    "                pre += 1\n",
    "            else:\n",
    "                pre1 += 1\n",
    "                ans = max(ans, min(pre, pre1))\n",
    "        return ans*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        if \"01\" not in s:\n",
    "            return 0\n",
    "\n",
    "        length = 1\n",
    "        temp = \"01\"        \n",
    "        while temp in s:\n",
    "            length += 1\n",
    "            temp = \"0\" * length + \"1\" * length\n",
    "        \n",
    "        return (length - 1) * 2             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        i = 0\n",
    "        sum0 = sum1 = 0\n",
    "\n",
    "        while i < len(s):\n",
    "            pre = i\n",
    "            while i < len(s) and s[i] == '0':\n",
    "                i+= 1\n",
    "                continue\n",
    "            sum0 = i-pre\n",
    "            pre = i\n",
    "            while i < len(s) and s[i] == '1':\n",
    "                i+=1\n",
    "                continue\n",
    "            sum1 = i-pre\n",
    "            res = max(res, 2*min(sum1, sum0))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0,0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1] += 1\n",
    "                res = max(res, 2 * min(count)) \n",
    "                \n",
    "            elif i == 0 or s[i-1] == '1':\n",
    "                count[0] = 1\n",
    "                count[1] = 0\n",
    "            else:\n",
    "                count[0] += 1\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        cnt0 = 0\n",
    "        cnt1 = 0\n",
    "        maxl = 0\n",
    "        for i in range(0, len(s)):\n",
    "            if s[i] == '0':\n",
    "                if cnt1 == 0:\n",
    "                    cnt0 += 1\n",
    "                else:\n",
    "                    if cnt0 == cnt1:\n",
    "                        maxl = max(maxl, cnt0+cnt1)\n",
    "                    cnt0 = 1\n",
    "                    cnt1 = 0\n",
    "            elif s[i] == '1':\n",
    "                cnt1 += 1\n",
    "            \n",
    "            maxl = max(maxl, 2*min(cnt0, cnt1))\n",
    "\n",
    "        return maxl "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        count_0, count_1 = 0, 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i]== '0' and count_1 == 0:\n",
    "                count_0 += 1\n",
    "            elif s[i] == '0' and count_1 != 0:\n",
    "                ans = max(ans, 2*min(count_0, count_1))\n",
    "                count_0 = 1\n",
    "                count_1 = 0\n",
    "            elif s[i] == '1' and count_0 != 0:\n",
    "                count_1 += 1\n",
    "        return max(ans, 2*(min(count_0, count_1)))\n",
    "\n",
    "\n",
    "             \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        str_index_0, str_index_1= \"0\", \"1\"\n",
    "        flag = True\n",
    "        count = 1\n",
    "        while flag:\n",
    "            if str_index_0 * count + str_index_1 * count not in s:\n",
    "               return (count-1) * 2\n",
    "            else:\n",
    "                count += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        tmp = \"\"\n",
    "\n",
    "        while len(tmp) < len(s) and tmp in s:\n",
    "            tmp = '0' + tmp + '1'\n",
    "            if tmp not in s:\n",
    "                return len(tmp) - 2\n",
    "        return len(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        def help(s: str):\n",
    "            if len(s) % 2 != 0:\n",
    "                return False\n",
    "\n",
    "            return s == \"0\" * (len(s)//2) + \"1\" * (len(s)//2)\n",
    "\n",
    "        for sub_len in range(len(s), 0, -1):\n",
    "            for i in range(len(s) - sub_len + 1):\n",
    "                if help(s[i:i+sub_len]):\n",
    "                    return sub_len\n",
    "\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        return max(2 * i if (\"0\" * i + \"1\" * i) in s else 0 for i in range(len(s) // 2 + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        zero=0\n",
    "        one=0\n",
    "        maxleng =0\n",
    "        for c in s:\n",
    "            if c == '0':\n",
    "                if not one:\n",
    "                    zero += 1\n",
    "                if one:\n",
    "                    one = 0\n",
    "                    zero = 1\n",
    "            if zero and c == '1':\n",
    "                one +=1\n",
    "            if one <= zero:\n",
    "                maxleng = max(one * 2, maxleng)\n",
    "                if one == zero:\n",
    "                    one = 0\n",
    "                    zero =0\n",
    "        return maxleng\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        cnt0, cnt1 = 0, 0\n",
    "        res = 0\n",
    "        if s[0]=='0':\n",
    "            cnt0 += 1\n",
    "        for i in range(1,len(s)):\n",
    "            # 如果是10组合\n",
    "            if s[i-1]=='1' and s[i]=='0':\n",
    "                res = max(res, min(cnt0,cnt1)*2)\n",
    "                cnt0, cnt1 = 1, 0\n",
    "            else:\n",
    "                if s[i]=='0':\n",
    "                    cnt0 += 1\n",
    "                else:\n",
    "                    cnt1 += 1\n",
    "        return max(res, min(cnt0,cnt1)*2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        n, idx, ans = len(s), 0, 0\n",
    "        while idx < n:\n",
    "            a, b = 0, 0\n",
    "            while idx < n and s[idx] == '0':\n",
    "                a, idx = a + 1, idx + 1\n",
    "            while idx < n and s[idx] == '1':\n",
    "                b, idx = b + 1, idx + 1\n",
    "            ans = max(ans, min(a, b) * 2)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        s+='#'\n",
    "        counts=[0,0]\n",
    "        result=0\n",
    "        last=''\n",
    "        for c in s:\n",
    "            if c=='1':\n",
    "                counts[1]+=1\n",
    "            else:\n",
    "                if last=='1':\n",
    "                    if min(counts)*2>result:\n",
    "                        result=min(counts)*2\n",
    "                    counts[1]=0\n",
    "                    counts[0]=1\n",
    "                else:\n",
    "                    counts[0]+=1\n",
    "            last=c\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        index = 0\n",
    "        while index < len(s):\n",
    "            cnt_0 = 0\n",
    "            cnt_1 = 0\n",
    "            while index < len(s) and s[index] == \"0\":\n",
    "                cnt_0 += 1\n",
    "                index += 1\n",
    "            while index < len(s) and s[index] == \"1\":\n",
    "                cnt_1 += 1\n",
    "                index += 1\n",
    "            ans = max(ans, 2*min(cnt_0, cnt_1))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        count = [0,0]\n",
    "        for i in range(n):\n",
    "            if s[i] == '1':\n",
    "                count[1]+=1\n",
    "                res=max(res,2*min(count))\n",
    "            elif i==0 or s[i-1]=='1':\n",
    "                count[0]=1\n",
    "                count[1]=0\n",
    "            else:\n",
    "                count[0]+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "\n",
    "        ptr = 0\n",
    "        ans = 0\n",
    "        while ptr < len(s):\n",
    "            if s[ptr] == '0':\n",
    "                zc = 0\n",
    "                while ptr < len(s) and s[ptr] == '0':\n",
    "                    ptr += 1\n",
    "                    zc += 1\n",
    "                oc = 0\n",
    "                while ptr < len(s) and s[ptr] == '1':\n",
    "                    oc += 1\n",
    "                    ptr += 1\n",
    "                ans = max(ans, min(oc, zc) * 2)\n",
    "            else:\n",
    "                ptr += 1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        def check(i: int, j: int) -> bool:\n",
    "            cnt = 0\n",
    "            for k in range(i, j + 1):\n",
    "                if s[k] == '1':\n",
    "                    cnt += 1\n",
    "                elif cnt:\n",
    "                    return False\n",
    "            return cnt * 2 == j - i + 1\n",
    "        \n",
    "        ans = 0\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if check(i, j):\n",
    "                    ans = max(ans, j - i + 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        res = '01'\n",
    "        # s = '01011000111'\n",
    "        if res not in s:\n",
    "            return(0)\n",
    "            # exit(0)\n",
    "        while res in s:\n",
    "            res = '0'+res+'1'\n",
    "            print(res)\n",
    "        return(len(res) - 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findTheLongestBalancedSubstring(self, s: str) -> int:\n",
    "        param = '01'\n",
    "\n",
    "        while param in s:\n",
    "                param = '0' + param + '1'\n",
    "        \n",
    "        return len(param)-2\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
